pkg_name=cross-gcc
version=4.7.4
build_dir=$builds_dir_root/$pkg_name-$version
src_dir=$src_dir_root/gcc-$version
url=ftp://ftp.lip6.fr/pub/gcc/releases/gcc-$version/gcc-$version.tar.bz2


################################################################################
#sysrooted gcc bugs:
#1
#mkdir -p "$sysroot/usr/include"
#2
#another bug of gcc? It *WANTS* sysroot usr/lib to go into usr/lib64 or
#usr/lib32 and then find crtX.o libc objects. We do the same for /lib.
#basically the default for the system is 64 bits
#ln -fs lib64 "$sysroot/usr/lib"
#ln -fs lib64 "$sysroot/lib"
################################################################################

rm -Rf "$src_dir"
cd "$src_dir_root"
tar jxf "gcc-$version.tar.bz2"

rm -Rf "$build_dir"
mkdir -p "$build_dir"
cd "$build_dir"

#HERE WE TRY TO GET OUT OF GCC SDK A BARE TARGET C COMPILER FOR THE TARGET LIBC
#AND THIS IS INSANE

#--with_newlib is *the* option to disable *SOME* and *NOT ALL* libc dependencies
#of libgcc. The worst option name *ever*.
#Additionnaly, gcc must not support decimal float, or libgcc will try to
#compile some support of it... using libc headers... OMG! This is sooo dirty...
#Then we pass --disable-decimal-float option which will be passed down to
#real gcc configure then libdecnumber to compile a "no support" of decimal
#number.
#*AGAIN*, we pass the --disable-threads option to the real gcc, or libgcc will
#try to compile some threading support using libc headers...
#Of course, they never really tested a bare minimal build for gcc, or they don't
#want people to easily build their system from scratch, I don't know where to
#set the cursor between the two: the "stack protector" code generation may be
#dependent on the not yet installed libc... but with a bare compiler, used to
#compile that libc, you must manually tell gcc if the futur libc will use the
#stack protector and if that libc will handle a part of it. Of course, gcc SDK
#does autodetect this feature and does not give manual control and then fail.
#Of course again, this the case for the most common libc, the glibc... Are gcc
#devs testing their SDK for LFS? I don't think so. Let's force it, uglily
#patching the configure script.
#It seems that if you have a libc in your sysroot, it will fail.
#*THIS IS NOT SERIOUS WORK, THIS IS COCKROACH DIARRHEA*.
sed -i 's/gcc_cv_libc_provides_ssp=no/gcc_cv_libc_provides_ssp=yes/' \
						"$src_dir/gcc/configure"


export CFLAGS=-O0
"$src_dir/configure"					\
	--target=$gnu_target				\
	--disable-shared				\
	"--prefix=$cross_dir_root"			\
	--enable-languages=c				\
	--enable-serial-target-configure		\
	"--with-gmp=$cross_dir_root"			\
	"--with-mpfr=$cross_dir_root"			\
	"--with-mpc=$cross_dir_root"			\
	"--with-build-time-tools=$cross_dir_root"	\
	"--with-sysroot=$sysroot"			\
	--with-native-system-header-dir=/include	\
	--without-headers				\
	--with-newlib					\
	--disable-decimal-float				\
	--disable-threads				\
	--with-gnu-ld					\
	--with-gnu-as					\
	--disable-multilib				\
	--disable-nls
unset CFLAGS

#build only a bare compiler, without its supporting libs since there is
#no libc yet to build those supporting libs against
make all-gcc
make all-target-libgcc

install_tmp_dir=/tmp/$pkg_name-$version
rm -Rf "$install_tmp_dir"
mkdir -p "$install_tmp_dir"

make install-gcc "DESTDIR=$install_tmp_dir"
make install-target-libgcc "DESTDIR=$install_tmp_dir"

install_dirs_with_prefix "$builders_dir_root/$pkg_name/runtime.dirs" "$cross_dir_root/"
install_files_with_prefix "$builders_dir_root/$pkg_name/runtime.files" "$install_tmp_dir" "$cross_dir_root/"
install_links_with_prefix "$builders_dir_root/$pkg_name/runtime.links" "$cross_dir_root/"
install_dirs_with_prefix  "$builders_dir_root/$pkg_name/devel.dirs" "$cross_dir_root/"
install_files_with_prefix "$builders_dir_root/$pkg_name/devel.files" "$install_tmp_dir" "$cross_dir_root/"
install_links_with_prefix "$builders_dir_root/$pkg_name/devel.links" "$cross_dir_root/"
elf_strip_with_prefix "$builders_dir_root/$pkg_name/elf-strip" "$cross_dir_root/"

rm -Rf "$install_tmp_dir"
rm -Rf "$build_dir"
rm -Rf "$src_dir"
